#判断某个名字是否存在
names=["张三","李四","王五","赵六"]
key=input("请输入需要查询的姓名:")
result="不存在" #保存结果
for name in names:
if name==key:
result="存在"
print(result)
返回值:
请输入需要查询的姓名:大宝
不存在
请输入需要查询的姓名:张三
存在
#判断某个名字是否存在
scores=[34,68,44,90,86,74,88,100,47]
count=0 #声明变量并赋值
for score in scores:
if score>=60:
count=count+1
print("合格人数是:",count,"人,合率格是",round(count/len(scores),4))
返回值:
合格人数是: 6 人,合率格是 0.6667
#苹果打8折,其他商品不打折,求购物车总价
cart={"apple":25,"banana":12,"orange":9}
totalPrice=0 #声明变量并赋值
for key in cart:
name=key
price=cart[key]
if name=="apple":
price=price*0.8
totalPrice=totalPrice+price
print("购物车总价为:",totalPrice)
返回值:
购物车总价为: 41.0